<template>
  <div class="global-padding">
    <div style="margin-bottom: 10px;">
      <el-input v-model="searchValue" placeholder="请输入内容" style="width: 200px;margin-right: 10px;" />
      <el-date-picker
        v-model="datePickerValue"
        type="daterange"
        align="right"
        unlink-panels
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="margin-right: 10px;"
        @change="datePickerConfirm"
      />
      <el-button type="primary" style="margin-left: 10px;" @click="conditionSearch">条件搜索</el-button>
      <el-button type="success" style="margin-left: 10px;" @click="handleAdd">添加开放城市</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="id"
        label="ID"
      />
      <el-table-column
        fixed
        prop="cityId"
        label="城市ID"
      />
      <el-table-column
        prop="cityName"
        label="城市名称"
      />
      <el-table-column
        fixed="right"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="confirmDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加" :visible.sync="addDialogFormVisible">
      <el-form>
        <el-form-item label="城市">
          <el-autocomplete
            v-model="addValue"
            class="inline-input"
            :fetch-suggestions="querySearch"
            placeholder="请输入城市名称后选择"
            :trigger-on-focus="false"
            @select="handleSelect"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑" :visible.sync="updateDialogFormVisible">
      <el-form>
        <el-form-item label="城市">
          <el-autocomplete
            v-model="updateValue"
            class="inline-input"
            :fetch-suggestions="querySearch2"
            placeholder="请输入城市名称后选择"
            :trigger-on-focus="false"
            @select="handleSelect2"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmUpdate">确 定</el-button>
      </div>
    </el-dialog>
    <div style="margin-top: 10px;text-align: center;">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="curPage"
        :page-size="pageSize"
        :total="total"
        @current-change="curPageChange"
      />
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  data() {
    return {
      optionValue: '',
      curPage: 1,
      pageSize: 8,
      total: 1,
      tableData: [],
      dateField: 'create_time',
      datePickerValue: '',
      searchField: 'city_name',
      searchValue: '',
      curRow: {},
      updateDialogFormVisible: false,
      addDialogFormVisible: false,
      addValue: '',
      addCityId: '',
      updateValue: '',
      updateCityId: ''
    }
  },
  mounted() {
    this.reqListData()
  },
  methods: {
    handleUpdate(row) {
      console.log(row)
      this.curRow = row
      this.updateDialogFormVisible = true
    },
    curPageChange(v) {
      console.log(v)
      this.curPage = v
      this.reqListData()
    },
    datePickerConfirm(v) {
      console.log(v)
      console.log(v[0])
    },
    selectOptionChange(v) {
      console.log(v)
    },
    reqListData() {
      request.get('openCity/openCityList', { params: {
        curPage: this.curPage,
        pageSize: this.pageSize,
        dateField: this.dateField,
        startDate: this.datePickerValue[0],
        endDate: this.datePickerValue[1],
        searchField: this.searchField,
        search: this.searchValue
      }}).then(res => {
        this.tableData = []
        console.log(res)
        for (const v of res.records) {
          this.tableData.push(v)
        }
        this.curPage = res.current
        this.pageSize = res.size
        this.total = res.total
      })
    },
    conditionSearch() {
      this.reqListData()
    },
    confirmDelete(row) {
      this.$alert('确定删除吗', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          if (action === 'confirm') {
            request.delete(`/openCity/openCityDelete/${row.id}`).then(res => {
              this.$message({
                type: 'info',
                message: `删除成功`
              })
              this.reqListData()
            })
          }
        }
      })
    },
    handleConfirmUpdate() {
      this.updateDialogFormVisible = false
      request.put(`/openCity/openCityUpdate`, {
        id: this.curRow.id,
        cityId: this.updateCityId
      }).then(res => {
        this.$message({
          type: 'info',
          message: `更新成功`
        })
        this.reqListData()
      })
    },
    handleAdd() {
      this.addDialogFormVisible = true
    },
    handleConfirmAdd() {
      this.addDialogFormVisible = false
      request.post(`/openCity/openCityAdd`, {
        cityId: this.addCityId
      }).then(res => {
        this.$message({
          type: 'info',
          message: `添加成功`
        })
        this.addCityId = ''
        this.addValue = ''
        this.reqListData()
      })
    },
    querySearch(queryString, cb) {
      request.get(`/district/searchCity?cityName=${queryString}`).then(res => {
        const temp = []
        for (const v of res) {
          temp.push({
            value: v.name,
            id: v.id
          })
        }
        // 调用 callback 返回建议列表的数据
        cb(temp)
      })
    },
    handleSelect(item) {
      console.log(item)
      this.addCityId = item.id
    },
    querySearch2(queryString, cb) {
      request.get(`/district/searchCity?cityName=${queryString}`).then(res => {
        const temp = []
        for (const v of res) {
          temp.push({
            value: v.name,
            id: v.id
          })
        }
        // 调用 callback 返回建议列表的数据
        cb(temp)
      })
    },
    handleSelect2(item) {
      console.log(item)
      this.updateCityId = item.id
    }
  }
}
</script>
